﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OF.Security.Utils;

namespace OF.Security.ApiClient
{
    public class PassportClient
    {
        private static string ApiUrl = PassportSignInSettings.GetConfig().PassportApiUrlRoot;
        private static Encoding apiEncoding = Encoding.UTF8;
        public static APIBaseResponse<BizResponse<ResPassportUserInfo>> Authenticate(ReqPassportAuthenticate reqInfo)
        {
            APIBaseResponse<BizResponse<ResPassportUserInfo>> oReturn = new APIBaseResponse<BizResponse<ResPassportUserInfo>>();
            string szApi = ApiUrl + "/passport/Authenticate";
            //请求参数
            var szJosnParam = reqInfo.ToJSON();
            string szContent = "";
            if (RequestHelper.PostJsonToWebApi(szApi, szJosnParam, apiEncoding, out szContent))
            {
                var data =szContent.JSONToObject<BizResponse<ResPassportUserInfo>>();
                oReturn = oReturn.Success(data);
            }
            else
            {
                oReturn = oReturn.Error(szContent);
            }
            return oReturn;
        }

        public static APIBaseResponse<BizResponse<ResPassportUserInfo>> GetUserInfoByID(int userID)
        {
            APIBaseResponse<BizResponse<ResPassportUserInfo>> oReturn = new APIBaseResponse<BizResponse<ResPassportUserInfo>>();
            string szApi = ApiUrl + "/passport/GetUserInfoByID";
            //请求参数
            var param ="?userID="+userID;
            szApi = szApi + param;
            string szContent = "";
            if (RequestHelper.GetHtml(szApi, "get", apiEncoding, out szContent))
            {
                var data = szContent.JSONToObject<BizResponse<ResPassportUserInfo>>();
                oReturn = oReturn.Success(data);
            }
            else
            {
                oReturn = oReturn.Error(szContent);
            }
            return oReturn;
        }

        public static APIBaseResponse<BizResponse> AddPassportSignIn(ReqPassportSignIn reqInfo)
        {
            APIBaseResponse<BizResponse> oReturn = new APIBaseResponse<BizResponse>();
            string szApi = ApiUrl + "/passport/AddPassportSignIn";
            //请求参数
            var szJosnParam = reqInfo.ToJSON();
            string szContent = "";
            if (RequestHelper.PostJsonToWebApi(szApi, szJosnParam, apiEncoding, out szContent))
            {
                var data = szContent.JSONToObject<BizResponse>();
                oReturn = oReturn.Success(data);
            }
            else
            {
                oReturn = oReturn.Error(szContent);
            }
            return oReturn;
        }

        public static APIBaseResponse<BizResponse> AddPassportTicket(ReqPassportTicket reqInfo)
        {
            APIBaseResponse<BizResponse> oReturn = new APIBaseResponse<BizResponse>();
            string szApi = ApiUrl + "/passport/AddPassportTicket";
            //请求参数
            var szJosnParam = reqInfo.ToJSON();
            string szContent = "";
            if (RequestHelper.PostJsonToWebApi(szApi, szJosnParam, apiEncoding, out szContent))
            {
                var data = szContent.JSONToObject<BizResponse>();
                oReturn = oReturn.Success(data);
            }
            else
            {
                oReturn = oReturn.Error(szContent);
            }
            return oReturn;
        }

        public static APIBaseResponse<BizResponse<List<string>>> GetAllAppLogOffUrl(string sessionID)
        {
            APIBaseResponse<BizResponse<List<string>>> oReturn = new APIBaseResponse<BizResponse<List<string>>>();
            string szApi = ApiUrl + "/passport/GetAllAppLogOffUrl";
            //请求参数
            var param = "?sessionID=" + sessionID;
            szApi = szApi + param;
            string szContent = "";
            if (RequestHelper.GetHtml(szApi, "get", apiEncoding, out szContent))
            {
                var data = szContent.JSONToObject<BizResponse<List<string>>>();
                oReturn = oReturn.Success(data);
            }
            else
            {
                oReturn = oReturn.Error(szContent);
            }
            return oReturn;
        }




    }
}
